package com.example.smartcity.Controller;

import com.alibaba.fastjson.JSONObject;
import com.example.smartcity.Entity.User;
import com.example.smartcity.Entity.login;
import com.example.smartcity.Service.LoginAndRegisterService;
//import jakarta.servlet.http.HttpServletResponse;
import com.example.smartcity.Service.NewsCommentService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class LoginActivityController {
    @Autowired
    @Qualifier(value = "LoginAndRegisterService")
    private LoginAndRegisterService loginService;
    private NewsCommentService newsCommentService;

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(HttpServletResponse response, @RequestBody String loginJSON){
        System.out.println("login:");
        login log = new login();
        try{
            log = new ObjectMapper().readValue(loginJSON, login.class);
        }catch (Exception e){
            System.out.println(e);
        }
        return loginService.login(log,response);
    }

    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public String register(@RequestBody String userJSON){
        System.out.println(userJSON);

        User user = new User();
        try{
            user = new ObjectMapper().readValue(userJSON, User.class);
        }catch (Exception e){
            System.out.println(e);
        }

        return loginService.register(user);
    }


    @RequestMapping(value = "/test", method = RequestMethod.POST)
    public ResponseEntity<Map<String, Object>> test(@RequestBody String data) {
        // 在这里处理或验证数据
        // ...
        // 将处理后的数据放入一个Map中
        Map<String, Object> responseData = new HashMap<>();
        responseData.put("data", data);
        // 创建响应实体并设置响应体为处理后的数据
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        ResponseEntity<Map<String, Object>> response = new ResponseEntity<>(responseData, headers, HttpStatus.OK);
        System.out.println(response.toString());
        return response;
    }
    @RequestMapping(value = "/user/getavatar", method = RequestMethod.GET)
    public String getAvatar(@RequestParam("uid")int uid){
        return loginService.getUserAvatar(uid);
    }
    @RequestMapping(value = "/user/getuser", method = RequestMethod.GET)
    public String getUser(@RequestParam("uid")int uid){
        User user = loginService.getUserByUid(uid);
        JSONObject map = new JSONObject();
        map.put("nickname",user.getNickname());
        map.put("email",user.getEmail());
        map.put("sexual",user.getSexual());
        map.put("avatarurl",user.getAvatarurl());
        map.put("phonenumber",user.getPhonenubmber());
        map.put("license",user.getLicensenumber());
        return map.toJSONString();
    }
    @RequestMapping(value = "/user/updateuser",method =RequestMethod.POST)
    public String updateUser(@RequestBody String jsonStr) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);
        loginService.updateUserInfo(map);
        JSONObject  response = new JSONObject();
        response.put("msg","update success!!");
        response.put("code",200);
        return response.toJSONString();
    }
    @RequestMapping(value = "/user/getuserbyid",method =RequestMethod.GET)
    public String getUserById(@RequestParam("uid")int uid){
        User user = loginService.getUserByUid(uid);
        JSONObject map = new JSONObject();
        map.put("avatarurl",user.getAvatarurl());
        map.put("nickname",user.getNickname());
        map.put("email",user.getEmail());
        map.put("sexual",user.getSexual()+"");
        map.put("phonenubmber",user.getPhonenubmber());
        map.put("licensenumber",user.getLicensenumber());
        return map.toJSONString();
    }
    @RequestMapping(value = "/user/resetPWD")
    public String resetPWD(@RequestBody String jsonStr) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        Map<String, Object> map = objectMapper.readValue(jsonStr, Map.class);

        System.out.println("老密码"+map.get("oldPassword")+","+"新密码"+map.get("password")+"用户名"+map.get("username"));
        loginService.updateUserPassword(map);
        JSONObject  response = new JSONObject();
        response.put("msg","密码修改成功");
        response.put("code",200);
        return response.toString();
    }
    @RequestMapping(value = "/user/collection")
    public String collection(@RequestParam("uid")String uid,@RequestParam("articlename")String title){
        int uuid = Integer.valueOf(uid);
        JSONObject map = new JSONObject();
        if(uuid == 1){
            map.put("code",500);
            map.put("msg","请先登录");
            return map.toJSONString();
        }
        int newsid = newsCommentService.findNewsIdByTitle(title);
        loginService.setNewCollection(uuid,newsid);
        map.put("code",200);
        map.put("msg","收藏成功");
        return map.toJSONString();
    }



}
